package com.skyz.wrap.utils;

import java.math.BigDecimal;

public class BigDecimalUtil {
    public static  String formatZero(String str){
        BigDecimal zero = BigDecimal.ZERO;

        BigDecimal big = new BigDecimal(str);
        BigDecimal bigZero = null;
        if (big.compareTo(zero) == 0) {
            bigZero= zero;
        }else {
            bigZero =  big.stripTrailingZeros();
        }
        return  bigZero.toPlainString();
    }

    public static  String formatValueTransfer(double str){
        double value = double2Scale(str,4);
        String zeroStr =formatZero(String.valueOf(value));
        return  zeroStr;
    }

    public static double double2Scale(double value,int scale){
        BigDecimal two = new BigDecimal(value+0.000001);
        double three = two.setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();
        return three;
    }

    /**
     * 默认保留位数
     * @param value
     * @return
     */
    public static double double2DefScale(double value){
        return double2Scale(value,4);
    }
}
